package com.stu.utils;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

/**
 * 有关数据库操作的工具类
 */
public class JDBCUtil {
    private static QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

    public static int update(String sql, Object... params){
        try {
            return queryRunner.update(sql, params);
        } catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    public static <T> T queryOne(String sql, Class<T> clazz, Object... params){
        try {
            return queryRunner.query(sql, new BeanHandler<T>(clazz), params);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static <T> List<T> queryList(String sql, Class<T> clazz, Object... params){
        try {
            return queryRunner.query(sql, new BeanListHandler<T>(clazz), params);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
